<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="baidu-site-verification" content="vvVgSQUzPU" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1"
    />
    <meta name="keywords" content="虎虎|博客|个人|utone|Utone|">
    <meta name="description" content="骑士恨煎蛋，骑士恨孜然">
    
    <title>
       ydwelua -  FBICirno In Here
    </title>
    
<link rel="stylesheet" href="/myfreeblog/css/style.css">
 
    
      <link rel="icon" href="/images/favicon.ico" />
    
  <meta name="generator" content="Hexo 4.2.0"></head>
  <body>
    <div id="container">
      <div id="main-left">
        <div id="introduce">
          <div class="logo-worp">
            <a href="/"><img class="logo" src="/images/logo.png" alt=""/></a>
          </div>
          <ul>
            
              <li><a href="/myfreeblog/"> 首页 </a></li>
            
              <li><a href="/myfreeblog/archives"> 归档 </a></li>
            
              <li><a href="/myfreeblog/about"> 关于 </a></li>
            
              <li><a href="/myfreeblog/atom.xml"> RSS </a></li>
            
            <li>
              <img id="search-img" src="/images/search.svg" alt="search" />
            </li>
          </ul>
           
            <ol class="post-toc"><li class="post-toc-item post-toc-level-1"><a class="post-toc-link" href="#ydwe-lua引擎使用说明"><span class="post-toc-number">1.</span> <span class="post-toc-text">ydwe lua引擎使用说明</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#简介"><span class="post-toc-number">1.1.</span> <span class="post-toc-text">简介</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#入口"><span class="post-toc-number">1.2.</span> <span class="post-toc-text">入口</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#lua引擎对标准lua的修改"><span class="post-toc-number">1.3.</span> <span class="post-toc-text">lua引擎对标准lua的修改</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#内置库"><span class="post-toc-number">1.4.</span> <span class="post-toc-text">内置库</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-common"><span class="post-toc-number">1.5.</span> <span class="post-toc-text">jass.common</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-ai"><span class="post-toc-number">1.6.</span> <span class="post-toc-text">jass.ai</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-globals"><span class="post-toc-number">1.7.</span> <span class="post-toc-text">jass.globals</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-japi"><span class="post-toc-number">1.8.</span> <span class="post-toc-text">jass.japi</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-hook"><span class="post-toc-number">1.9.</span> <span class="post-toc-text">jass.hook</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-slk"><span class="post-toc-number">1.10.</span> <span class="post-toc-text">jass.slk</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-runtime"><span class="post-toc-number">1.11.</span> <span class="post-toc-text">jass.runtime</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#jass-runtime库可以在地图运行时获取lua引擎的信息或修改lua引擎的部分配置。"><span class="post-toc-number">1.11.1.</span> <span class="post-toc-text">jass.runtime库可以在地图运行时获取lua引擎的信息或修改lua引擎的部分配置。</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#runtime-console-默认为false"><span class="post-toc-number">1.11.1.1.</span> <span class="post-toc-text">runtime.console(默认为false)</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#runtime-version"><span class="post-toc-number">1.11.1.2.</span> <span class="post-toc-text">runtime.version</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#runtime-error-handle"><span class="post-toc-number">1.11.1.3.</span> <span class="post-toc-text">runtime.error_handle</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#runtime-handle-level-默认为0"><span class="post-toc-number">1.11.1.4.</span> <span class="post-toc-text">runtime.handle_level(默认为0)</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#0-handle直接使用number，jass无法了解你在lua中对这个handle的引用情况，也不会通过增加引用计数来保护这个handle"><span class="post-toc-number">1.11.1.4.1.</span> <span class="post-toc-text">0: handle直接使用number，jass无法了解你在lua中对这个handle的引用情况，也不会通过增加引用计数来保护这个handle</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#1-handle封装在lightuserdata中，保证handle不能和整数相互转换，同样不支持引用计数"><span class="post-toc-number">1.11.1.4.2.</span> <span class="post-toc-text">1: handle封装在lightuserdata中，保证handle不能和整数相互转换，同样不支持引用计数</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#2-handle封装在userdata中，lua持有该handle时将增加handle的引用计数。lua释放handle时会释放handle的引用计数。"><span class="post-toc-number">1.11.1.4.3.</span> <span class="post-toc-text">2: handle封装在userdata中，lua持有该handle时将增加handle的引用计数。lua释放handle时会释放handle的引用计数。</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#runtime-sleep-默认为false"><span class="post-toc-number">1.11.1.5.</span> <span class="post-toc-text">runtime.sleep(默认为false)</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#runtime-catch-crash-默认为true"><span class="post-toc-number">1.11.1.6.</span> <span class="post-toc-text">runtime.catch_crash(默认为true)</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-console"><span class="post-toc-number">1.12.</span> <span class="post-toc-text">jass.console</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-3"><a class="post-toc-link" href="#jass-console与控制台相关"><span class="post-toc-number">1.12.1.</span> <span class="post-toc-text">jass.console与控制台相关</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#console-enable-默认为false"><span class="post-toc-number">1.12.1.1.</span> <span class="post-toc-text">console.enable(默认为false)</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#console-write"><span class="post-toc-number">1.12.1.2.</span> <span class="post-toc-text">console.write</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#console-read"><span class="post-toc-number">1.12.1.3.</span> <span class="post-toc-text">console.read</span></a></li></ol></li></ol></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-debug"><span class="post-toc-number">1.13.</span> <span class="post-toc-text">jass.debug</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-log"><span class="post-toc-number">1.14.</span> <span class="post-toc-text">jass.log</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-message"><span class="post-toc-number">1.15.</span> <span class="post-toc-text">jass.message</span></a></li><li class="post-toc-item post-toc-level-2"><a class="post-toc-link" href="#jass-bignum"><span class="post-toc-number">1.16.</span> <span class="post-toc-text">jass.bignum</span></a></li></ol></li></ol> 
          
        </div>
      </div>
      <div id="main-right"><div id="post">
    
    <header class="article-header">
        <h1>
            ydwelua
        </h1>
    </header>
    
    <div class="post-meta">
        <time class="post-data" datetime="2020-05-12T18:03:18.560Z" itemprop="datePublished">
            2020-05-13
        </time>
        
        
    </div>
    
    <div class="post-entry">
        <h1 id="ydwe-lua引擎使用说明"><a href="#ydwe-lua引擎使用说明" class="headerlink" title="ydwe lua引擎使用说明"></a>ydwe lua引擎使用说明</h1><h2 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h2><p>ydwe lua引擎(以下简称lua引擎)是一个嵌入到《魔兽争霸III》(以下简称魔兽)中的一个插件，它可以让魔兽可以执行lua并且调用魔兽的导出函数(在common.j内定义的函数)，就像使用jass那样。本说明假定你已经掌握了jass和lua的相关语法，有关语法的问题不再另行解释。</p>
<h2 id="入口"><a href="#入口" class="headerlink" title="入口"></a>入口</h2><p>在jass内调用 <code>call Cheat(&quot;exec-lua: hello&quot;)</code>，这等价于在lua里调用了 <code>require &#39;hello&#39;</code> 。lua引擎已经把地图内的文件加载到搜索路径，所以地图内的hello.lua将会得到执行。</p>
<h2 id="lua引擎对标准lua的修改"><a href="#lua引擎对标准lua的修改" class="headerlink" title="lua引擎对标准lua的修改"></a>lua引擎对标准lua的修改</h2><p>为了适合在魔兽内使用lua引擎对lua略有修改。 </p>
<ol>
<li>math.randomseed改为使用jass函数SetRandomSeed实现。</li>
<li>math.random改为使用jass函数GetRandomReal实现。</li>
<li>table元素随机化种子依赖于魔兽内部的随机种子。</li>
<li>屏蔽了部分被认为不安全的函数</li>
</ol>
<h2 id="内置库"><a href="#内置库" class="headerlink" title="内置库"></a>内置库</h2><p>lua引擎一共有12个内置库，可以通过”require ‘库名’”调用。  </p>
<ul>
<li>jass.common</li>
<li>jass.ai</li>
<li>jass.globals</li>
<li>jass.japi</li>
<li>jass.hook</li>
<li>jass.runtime</li>
<li>jass.slk</li>
<li>jass.console</li>
<li>jass.debug</li>
<li>jass.log</li>
<li>jass.message</li>
<li>jass.bignum</li>
</ul>
<h2 id="jass-common"><a href="#jass-common" class="headerlink" title="jass.common"></a>jass.common</h2><p>jass.common库包含common.j内注册的所有函数。 </p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> jass = <span class="built_in">require</span> <span class="string">'jass.common'</span></span><br><span class="line"><span class="built_in">print</span>(jass.GetHandleId(jass.Player(<span class="number">0</span>)))</span><br></pre></td></tr></table></figure>

<h2 id="jass-ai"><a href="#jass-ai" class="headerlink" title="jass.ai"></a>jass.ai</h2><p>jass.ai库包含common.ai内注册的所有函数。 </p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> jass = <span class="built_in">require</span> <span class="string">'jass.common'</span></span><br><span class="line"><span class="keyword">local</span> ai = <span class="built_in">require</span> <span class="string">'jass.ai'</span></span><br><span class="line"><span class="built_in">print</span>(ai.UnitAlive(jass.GetTriggerUnit()))</span><br></pre></td></tr></table></figure>

<h2 id="jass-globals"><a href="#jass-globals" class="headerlink" title="jass.globals"></a>jass.globals</h2><p>jass.globals库可以让你访问到jass内的全局变量。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> g = <span class="built_in">require</span> <span class="string">'jass.globals'</span></span><br><span class="line">g.udg_code = <span class="function"><span class="keyword">function</span><span class="params">()</span></span> <span class="comment">--将一个jass中定义的code变量赋值为一个lua函数</span></span><br><span class="line">	<span class="built_in">print</span>(jass.udg_strings[<span class="number">2</span>]) <span class="comment">--获取jass中定义的string数组</span></span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<p>你可以通过这个方式来实现和jass脚本之间的数据交互  </p>
<p>注意，你无法通过这个方式来访问jass中的自定义函数。也就是说，你无法带着参数来调用对方的函数，但通过code变量可以不带参数的来调用  </p>
<p>在上面的例子中,你可以在jass中执行:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">call TimerStart(CreateTimer(), 1, true, udg_code)</span><br></pre></td></tr></table></figure>

<p>这个计时器将在每次到期时正确的回调你在lua中定义的函数</p>
<h2 id="jass-japi"><a href="#jass-japi" class="headerlink" title="jass.japi"></a>jass.japi</h2><p>jass.japi库当前已经注册的所有japi函数。  </p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> jass = <span class="built_in">require</span> <span class="string">'jass.common'</span></span><br><span class="line"><span class="keyword">local</span> japi = <span class="built_in">require</span> <span class="string">'jass.japi'</span></span><br><span class="line">japi.EXDisplayChat(jass.Player(<span class="number">0</span>), <span class="number">0</span>, <span class="string">"Hello!"</span>)</span><br></pre></td></tr></table></figure>

<p>japi函数不同环境下可能会略有不同，你可以通过pairs遍历当前的所有japi函数</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> <span class="built_in">pairs</span>(<span class="built_in">require</span> <span class="string">'jass.japi'</span>) <span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span>(k, v)</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<h2 id="jass-hook"><a href="#jass-hook" class="headerlink" title="jass.hook"></a>jass.hook</h2><p>jass.hook库可以对common.j内注册的函数下钩子。注：jass.common库不会受到影响。  </p>
<p>同时，为了避免jass和lua之间传递浮点数时产生误差，通过jass.hook传递到lua中的浮点数，并不是number类型，而是userdata。当你需要<strong>精确</strong>地操纵浮点数时，也请注意这点。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> hook = <span class="built_in">require</span> <span class="string">'jass.hook'</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">hook.CreateUnit</span><span class="params">(pid, uid, x, y, face, realCreateUnit)</span></span></span><br><span class="line">	<span class="comment">-- 当jass内调用CreateUnit时，就会被执行</span></span><br><span class="line">	<span class="built_in">print</span>(<span class="string">'CreateUnit'</span>)</span><br><span class="line">	<span class="built_in">print</span>(<span class="built_in">type</span>(x))</span><br><span class="line">	<span class="keyword">return</span> realCreateUnit(pid, uid, x, y, face)</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<h2 id="jass-slk"><a href="#jass-slk" class="headerlink" title="jass.slk"></a>jass.slk</h2><p>jass.slk库可以在地图运行时读取地图内的slk/w3*文件。  </p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> slk = <span class="built_in">require</span> <span class="string">'jass.slk'</span></span><br><span class="line"><span class="built_in">print</span>(slk.ability.AHbz.Name)</span><br></pre></td></tr></table></figure>

<p>你也可以遍历一个表或者一个物体</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> slk = <span class="built_in">require</span> <span class="string">'jass.slk'</span></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> <span class="built_in">pairs</span>(slk.ability) <span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span>(k .. <span class="string">' '</span> .. v)</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> <span class="built_in">pairs</span>(slk.ability.AHbz) <span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span>(k .. <span class="string">' '</span> .. v)</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<p>slk包含</p>
<ul>
<li>unit</li>
<li>item</li>
<li>destructable</li>
<li>doodad</li>
<li>ability</li>
<li>buff</li>
<li>upgrade</li>
<li>misc</li>
</ul>
<p>与你物体编辑器中的项目一一对应。  </p>
<p>获取数据时使用的索引你可以在物体编辑器中通过Ctrl+D来查询到  </p>
<p>注意，当访问正确时返回值永远是字符串。如果你获取的是某个单位的生命值，你可能需要使用tonumber来进行转换。当访问不正确时将返回nil。</p>
<h2 id="jass-runtime"><a href="#jass-runtime" class="headerlink" title="jass.runtime"></a>jass.runtime</h2><h3 id="jass-runtime库可以在地图运行时获取lua引擎的信息或修改lua引擎的部分配置。"><a href="#jass-runtime库可以在地图运行时获取lua引擎的信息或修改lua引擎的部分配置。" class="headerlink" title="jass.runtime库可以在地图运行时获取lua引擎的信息或修改lua引擎的部分配置。"></a>jass.runtime库可以在地图运行时获取lua引擎的信息或修改lua引擎的部分配置。</h3><figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> runtime = <span class="built_in">require</span> <span class="string">'jass.runtime'</span></span><br></pre></td></tr></table></figure>

<h4 id="runtime-console-默认为false"><a href="#runtime-console-默认为false" class="headerlink" title="runtime.console(默认为false)"></a>runtime.console(默认为false)</h4><p>赋值为true后会打开一个cmd窗口，print与console.write函数可以输出到这里</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">runtime.console = <span class="literal">true</span></span><br></pre></td></tr></table></figure>

<h4 id="runtime-version"><a href="#runtime-version" class="headerlink" title="runtime.version"></a>runtime.version</h4><p>返回当前lua引擎的版本号</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(runtime.version)</span><br></pre></td></tr></table></figure>

<h4 id="runtime-error-handle"><a href="#runtime-error-handle" class="headerlink" title="runtime.error_handle"></a>runtime.error_handle</h4><p>当你的lua脚本出现错误时将会调用此函数。</p>
<p>runtime.error_handle有一个默认值，等价于以下函数</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">runtime.error_handle = <span class="function"><span class="keyword">function</span><span class="params">(msg)</span></span></span><br><span class="line">	<span class="built_in">print</span>(<span class="string">"Error: "</span>, msg, <span class="string">"\n"</span>)</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<p>你也可以让它输出更多的信息，比如输出错误时的调用栈</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">runtime.error_handle = <span class="function"><span class="keyword">function</span><span class="params">(msg)</span></span></span><br><span class="line">	<span class="built_in">print</span>(<span class="string">"---------------------------------------"</span>)</span><br><span class="line">	<span class="built_in">print</span>(<span class="string">"              LUA ERROR!!              "</span>)</span><br><span class="line">	<span class="built_in">print</span>(<span class="string">"---------------------------------------"</span>)</span><br><span class="line">	<span class="built_in">print</span>(<span class="built_in">tostring</span>(msg) .. <span class="string">"\n"</span>)</span><br><span class="line">	<span class="built_in">print</span>(<span class="built_in">debug</span>.<span class="built_in">traceback</span>())</span><br><span class="line">	<span class="built_in">print</span>(<span class="string">"---------------------------------------"</span>)</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<p>注意，注册此函数后lua脚本的效率会降低(即使并没有发生错误)。</p>
<h4 id="runtime-handle-level-默认为0"><a href="#runtime-handle-level-默认为0" class="headerlink" title="runtime.handle_level(默认为0)"></a>runtime.handle_level(默认为0)</h4><p>lua引擎处理的handle的安全等级，有效值为0~2，注，等级越高，效率越低，安全性越高、</p>
<h5 id="0-handle直接使用number，jass无法了解你在lua中对这个handle的引用情况，也不会通过增加引用计数来保护这个handle"><a href="#0-handle直接使用number，jass无法了解你在lua中对这个handle的引用情况，也不会通过增加引用计数来保护这个handle" class="headerlink" title="0: handle直接使用number，jass无法了解你在lua中对这个handle的引用情况，也不会通过增加引用计数来保护这个handle"></a>0: handle直接使用number，jass无法了解你在lua中对这个handle的引用情况，也不会通过增加引用计数来保护这个handle</h5><figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> t = jass.CreateTimer()</span><br><span class="line"><span class="built_in">print</span>(t) <span class="comment">-- 1048000</span></span><br><span class="line"><span class="built_in">type</span>(t) <span class="comment">-- "number"</span></span><br></pre></td></tr></table></figure>

<h5 id="1-handle封装在lightuserdata中，保证handle不能和整数相互转换，同样不支持引用计数"><a href="#1-handle封装在lightuserdata中，保证handle不能和整数相互转换，同样不支持引用计数" class="headerlink" title="1: handle封装在lightuserdata中，保证handle不能和整数相互转换，同样不支持引用计数"></a>1: handle封装在lightuserdata中，保证handle不能和整数相互转换，同样不支持引用计数</h5><figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> t = jass.CreateTimer()</span><br><span class="line"><span class="built_in">print</span>(t) <span class="comment">-- "handle: 0x10005D"</span></span><br><span class="line"><span class="built_in">type</span>(t) <span class="comment">-- "userdata"</span></span><br><span class="line">jass.TimerStart(t, <span class="number">1</span>, <span class="literal">false</span>, <span class="number">0</span>) <span class="comment">-- ok</span></span><br></pre></td></tr></table></figure>

<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> t = jass.CreateTimer()</span><br><span class="line"><span class="keyword">local</span> h1 = jass.CreateTimer()</span><br><span class="line">jass.DestroyTimer(h1)</span><br><span class="line">jass.TimerStart(t, <span class="number">1</span>, <span class="literal">false</span>,</span><br><span class="line">	<span class="function"><span class="keyword">function</span><span class="params">()</span></span></span><br><span class="line">		<span class="keyword">local</span> h2 = jass.CreateTimer()</span><br><span class="line">		<span class="built_in">print</span>(h1) <span class="comment">-- "handle: 0x10005E"</span></span><br><span class="line">		<span class="built_in">print</span>(h2) <span class="comment">-- "handle: 0x10005E"</span></span><br><span class="line">	<span class="keyword">end</span></span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<h5 id="2-handle封装在userdata中，lua持有该handle时将增加handle的引用计数。lua释放handle时会释放handle的引用计数。"><a href="#2-handle封装在userdata中，lua持有该handle时将增加handle的引用计数。lua释放handle时会释放handle的引用计数。" class="headerlink" title="2: handle封装在userdata中，lua持有该handle时将增加handle的引用计数。lua释放handle时会释放handle的引用计数。"></a>2: handle封装在userdata中，lua持有该handle时将增加handle的引用计数。lua释放handle时会释放handle的引用计数。</h5><figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> t = jass.CreateTimer()</span><br><span class="line"><span class="keyword">local</span> h1 = jass.CreateTimer()</span><br><span class="line">jass.DestroyTimer(h1)</span><br><span class="line">jass.TimerStart(t, <span class="number">1</span>, <span class="literal">false</span>,</span><br><span class="line">	<span class="function"><span class="keyword">function</span><span class="params">()</span></span></span><br><span class="line">		<span class="keyword">local</span> h2 = jass.CreateTimer()</span><br><span class="line">		<span class="built_in">print</span>(h1) <span class="comment">-- "handle: 0x10005E"</span></span><br><span class="line">		<span class="built_in">print</span>(h2) <span class="comment">-- "handle: 0x10005F"</span></span><br><span class="line">	<span class="keyword">end</span></span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<h4 id="runtime-sleep-默认为false"><a href="#runtime-sleep-默认为false" class="headerlink" title="runtime.sleep(默认为false)"></a>runtime.sleep(默认为false)</h4><p>common.j中包含sleep操作的函数有4个，TriggerSleepAction/TriggerSyncReady/TriggerWaitForSound/SyncSelections。当此项为false时，lua引擎会忽略这4个函数的调用，并给予运行时警告。当此项为true时，这4个函数将会得到正确的执行。</p>
<p>但请注意此项为true时将降低lua引擎的运行效率，即使你没有使用这4个函数。</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> trg = jass.CreateTrigger()</span><br><span class="line"><span class="keyword">local</span> a = <span class="number">1</span></span><br><span class="line">jass.TriggerAddAction(trg, <span class="function"><span class="keyword">function</span><span class="params">()</span></span></span><br><span class="line">	jass.TriggerSleepAction(<span class="number">0.2</span>)</span><br><span class="line">	<span class="built_in">print</span>(a) <span class="comment">-- 2</span></span><br><span class="line"><span class="keyword">end</span>)</span><br><span class="line">jass.TriggerExecute(trg)</span><br><span class="line">a = <span class="number">2</span></span><br></pre></td></tr></table></figure>

<h4 id="runtime-catch-crash-默认为true"><a href="#runtime-catch-crash-默认为true" class="headerlink" title="runtime.catch_crash(默认为true)"></a>runtime.catch_crash(默认为true)</h4><p>调用jass.xxx/japi.xxx发生崩溃时，会生产一个lua错误，并忽略这个崩溃。你可以注册jass.runtime.error_handle来获得这个错误。注：开启此项会略微增加运行时消耗（即使没有发生错误）。</p>
<h2 id="jass-console"><a href="#jass-console" class="headerlink" title="jass.console"></a>jass.console</h2><h3 id="jass-console与控制台相关"><a href="#jass-console与控制台相关" class="headerlink" title="jass.console与控制台相关"></a>jass.console与控制台相关</h3><h4 id="console-enable-默认为false"><a href="#console-enable-默认为false" class="headerlink" title="console.enable(默认为false)"></a>console.enable(默认为false)</h4><p>赋值为true后会打开一个cmd窗口，print与console.write函数可以输出到这里</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">console.enable = <span class="literal">true</span></span><br></pre></td></tr></table></figure>

<h4 id="console-write"><a href="#console-write" class="headerlink" title="console.write"></a>console.write</h4><p>将utf8编码的字符串转化为ansi编码后输出到cmd窗口中，如果你需要输出魔兽中的中文，请使用该函数而不是print</p>
<h4 id="console-read"><a href="#console-read" class="headerlink" title="console.read"></a>console.read</h4><p>将控制台中的输入传入魔兽中(会自动转换编码)</p>
<p>首次调用console.read后将允许用户在控制台输入，输入完成后按回车键提交输入。</p>
<p>用户提交完成后，传入一个函数f来调用console.read，将会调用函数f，并将用户的输入作为参数传入(已转换为utf8编码)。</p>
<p>推荐的做法是每0.1秒运行一次console.read，见下面的例子：</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> jass    = <span class="built_in">require</span> <span class="string">'jass.common'</span></span><br><span class="line"><span class="keyword">local</span> console = <span class="built_in">require</span> <span class="string">'jass.console'</span></span><br><span class="line"></span><br><span class="line">console.<span class="built_in">write</span>(<span class="string">'测试开始...'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">--开启计时器,每0.1秒检查输入</span></span><br><span class="line">jass.TimerStart(jass.CreateTimer(), <span class="number">0.1</span>, <span class="literal">true</span>,</span><br><span class="line">	<span class="function"><span class="keyword">function</span><span class="params">()</span></span></span><br><span class="line"></span><br><span class="line">		<span class="comment">--检查CMD窗口中的用户输入,如果用户有提交了的输入,则回调函数(按回车键提交输入).否则不做任何动作</span></span><br><span class="line">		console.<span class="built_in">read</span>(</span><br><span class="line">			<span class="function"><span class="keyword">function</span><span class="params">(str)</span></span></span><br><span class="line">				<span class="comment">--参数即为用户的输入.需要注意的是这个函数调用是不同步的(毕竟其他玩家不知道你输入了什么)</span></span><br><span class="line">				jass.DisplayTimedTextToPlayer(jass.Player(<span class="number">0</span>), <span class="number">0</span>, <span class="number">0</span>, <span class="number">60</span>, <span class="string">'你在控制台中输入了:'</span> .. str)</span><br><span class="line">			<span class="keyword">end</span></span><br><span class="line">		)</span><br><span class="line">	<span class="keyword">end</span></span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p>需要注意的是控制台输入是不同步的。</p>
<h2 id="jass-debug"><a href="#jass-debug" class="headerlink" title="jass.debug"></a>jass.debug</h2><p>jass.debug库能帮助你更深入地剖析lua引擎的内部机制。</p>
<ul>
<li>functiondef jass.common或者jass.japi函数的定义</li>
</ul>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> jass = <span class="built_in">require</span> <span class="string">'jass.common'</span></span><br><span class="line"><span class="keyword">local</span> dbg = <span class="built_in">require</span> <span class="string">'jass.debug'</span></span><br><span class="line"><span class="built_in">print</span>(<span class="built_in">table</span>.<span class="built_in">unpack</span>(dbg.functiondef(jass.GetUnitX)))</span><br></pre></td></tr></table></figure>

<ul>
<li><p>globaldef jass.globals内值的定义</p>
</li>
<li><p>handledef handle对应对象的内部定义</p>
</li>
<li><p>currentpos 当前jass执行到的位置</p>
</li>
<li><p>handlemax jass虚拟机当前最大的handle</p>
</li>
<li><p>handlecount jass虚拟机当前的handle数</p>
</li>
<li><p>h2i/i2h handle和integer的转换，当你runtime.handle_level不是0时，你可能会需要它</p>
</li>
<li><p>handle_ref 增加handle的引用</p>
</li>
<li><p>handle_unref 减少handle的引用</p>
</li>
<li><p>gchash 指定一张table的gchash，gchash会决定了在其他table中这个table的排序次序<br>在默认的情况下，lua对table的排序次序是由随机数决定的，不同玩家的lua生成的随机数不一致，所以下面的代码在不同的玩家上执行的次序是不一致的，这可能会引起不同步掉线</p>
</li>
</ul>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> tbl = &#123;&#125;</span><br><span class="line"><span class="keyword">for</span> i = <span class="number">1</span>, <span class="number">10</span> <span class="keyword">do</span></span><br><span class="line">	<span class="keyword">local</span> k = &#123; id = i &#125;</span><br><span class="line">	tbl[k] = i</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> <span class="built_in">pairs</span>(tbl) <span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span>(k.id)</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<p>加上如果指定了gchash，那么它的次序就可以固定了</p>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> dbg = <span class="built_in">require</span> <span class="string">'jass.debug'</span></span><br><span class="line"><span class="keyword">local</span> tbl = &#123;&#125;</span><br><span class="line"><span class="keyword">for</span> i = <span class="number">1</span>, <span class="number">10</span> <span class="keyword">do</span></span><br><span class="line">	<span class="keyword">local</span> k = &#123; id = i &#125;</span><br><span class="line">	dbg.gchash(k, i)</span><br><span class="line">	tbl[k] = i</span><br><span class="line"><span class="keyword">end</span></span><br><span class="line"><span class="keyword">for</span> k, v <span class="keyword">in</span> <span class="built_in">pairs</span>(tbl) <span class="keyword">do</span></span><br><span class="line">	<span class="built_in">print</span>(k.id)</span><br><span class="line"><span class="keyword">end</span></span><br></pre></td></tr></table></figure>

<h2 id="jass-log"><a href="#jass-log" class="headerlink" title="jass.log"></a>jass.log</h2><p>日志库</p>
<ul>
<li>path 日志的输出路径</li>
<li>level 日志的等级，指定等级以上的日志才会输出</li>
<li>日志有6个等级 trace、debug、info、warn、error、fatal</li>
</ul>
<figure class="highlight lua"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">local</span> <span class="built_in">log</span> = <span class="built_in">require</span> <span class="string">'jass.log'</span></span><br><span class="line"><span class="built_in">log</span>.info(<span class="string">'这是一行日志'</span>)</span><br><span class="line"><span class="built_in">log</span>.<span class="built_in">error</span>(<span class="string">'这是一行'</span>, <span class="string">'日志'</span>)</span><br></pre></td></tr></table></figure>

<h2 id="jass-message"><a href="#jass-message" class="headerlink" title="jass.message"></a>jass.message</h2><ul>
<li>keyboard 一张表，魔兽的键盘码</li>
<li>mouse 本地玩家的鼠标坐标(游戏坐标)</li>
<li>button 本地玩家技能按钮的状态</li>
<li>hook 魔兽的消息回调，可以获得部分鼠标和键盘消息</li>
<li>selection 获得本地玩家当前选中单位</li>
<li>order_immediate 发布本地命令，无目标</li>
<li>order_point 发布本地命令，点目标</li>
<li>order_target 发布本地命令，单位目标</li>
<li>order_enable_debug 开启后，会在控制台打印当前的本地命令，调试用</li>
</ul>
<h2 id="jass-bignum"><a href="#jass-bignum" class="headerlink" title="jass.bignum"></a>jass.bignum</h2><p>加密算法库</p>

    </div>
    <!--畅言PC和WAP自适应版-->
  
</div></div>
    </div>
    <div id="footer">骑士恨煎蛋，骑士恨孜然</div>
    <img id="top" src="/images/up-circle.svg" alt="top" />
    
  </body>
</html>
<!-- 加载主题脚本文件 -->

<script src="/myfreeblog/scripts/utone.js"></script>


<script type="text/javascript">
  window.onload = function() {
    scroll.init()
    siteSearch.init()
  }
</script>
